Skip to content

Conversation

@MrPunyapal
Copy link
Contributor

@MrPunyapal MrPunyapal commented Aug 30, 2025

This pull request introduces a new Rector rule that automatically converts direct array access to use Laravel's Arr::get() method, improving code consistency and maintainability when working with nested arrays. The implementation includes comprehensive support for both simple array access and null coalesce patterns, with smart handling of edge cases.

New Rector Rule: Array Access to Arr::get() Conversion

  • Added ArrayToArrGetRector rule to convert array access patterns:

    • Simple access: $array['key']\Illuminate\Support\Arr::get($array, 'key')
    • Nested access: $array['nested']['key']\Illuminate\Support\Arr::get($array, 'nested.key')
    • With defaults: $array['key'] ?? 'default'\Illuminate\Support\Arr::get($array, 'key', 'default')
    • Preserves throw expressions: $array['key'] ?? throw new Exception() (unchanged)
  • The rule handles both scalar string and integer keys, building proper dot notation for nested access

  • Skips dynamic keys, complex expressions, and null coalesce with throw statements to maintain safety and original behavior

Example

-$array['key'];
-$array['nested']['key'];
-$array['key'] ?? 'default';
-$array['nested']['key'] ?? 'default';
+\Illuminate\Support\Arr::get($array, 'key');
+\Illuminate\Support\Arr::get($array, 'nested.key');
+\Illuminate\Support\Arr::get($array, 'key', 'default');
+\Illuminate\Support\Arr::get($array, 'nested.key', 'default');
$array['key'] ?? throw new Exception('Required');

@MrPunyapal
Copy link
Contributor Author

What do you say? @peterfox

@Hackbard
Copy link

Great! Do it

@calebdw
Copy link
Contributor

calebdw commented Sep 3, 2025

Not sure I really care for this rule for non-nested array access? Why nearly double the number of characters needed?

-$array['key'];
+data_get($array, 'key');

Now, converting a null coalesce could be useful:

-$array['key'] ?? null;
+data_get($array, 'key');
-$array['key'] ?? 'foo';
+data_get($array, 'key', 'foo');

@MrPunyapal
Copy link
Contributor Author

I'm open to making it for Arr::get().

@peterfox peterfox self-requested a review September 20, 2025 17:26
@peterfox
Copy link
Collaborator

peterfox commented Sep 20, 2025

@MrPunyapal sorry for my slowness. Been busy of late. The rule looks really good. I agree, it might be better to use Arr::get() instead of data_get as data_get is designed to work with objects as well. I can see this being useful as it is and we can look at expanding it to handle coalescing for defaults as well.

There's some useful code here as well I'd like to put into their own classes but that can be done later once this is merged.

With that in mind, would you be okay to make the change to Arr::get?

@MrPunyapal MrPunyapal changed the title feat: add ArrayToDataGetRector feat: add ArrayToArrGetRector Sep 20, 2025
@MrPunyapal
Copy link
Contributor Author

@peterfox updated things based on your suggestions.
let me know if you need any changes

@MrPunyapal MrPunyapal force-pushed the feat/array-to-data_get-rule branch from 4bf9ecb to afb6d38 Compare September 20, 2025 18:39
Copy link
Collaborator

@peterfox peterfox left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @MrPunyapal this looks good to me

@peterfox peterfox merged commit 38bc7af into driftingly:main Sep 22, 2025
5 checks passed
@wlkns
Copy link

wlkns commented Sep 22, 2025

I don't understand. Why introduce slowness and pull away from core PHP just for the sake of using Laravel methods?

Did anyone benchmark this on a large scale?

@peterfox
Copy link
Collaborator

I don't understand. Why introduce slowness and pull away from core PHP just for the sake of using Laravel methods?

Did anyone benchmark this on a large scale?

  1. People don't have to use it
  2. Some people might want to use it for reasons you might not get right away
  3. I'd use this for some readability personally

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants